﻿//  Copyright (c) 2010 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;

namespace DNA.Mvc.Community
{
    /// <summary>
    /// Present the forum group object.
    /// </summary>
    public class ForumGroup
    {
        private IForumContext context;

        private Forum thisForum;

        public virtual IForumContext Context { get { return context; } }

        public int ID { get; set; }

        public virtual string Title { get; set; }

        public virtual string Description { get; set; }

        public virtual int ParentID { get; set; }

        public virtual int Pos { get; set; }

        //public ForumGroup() { }

        public ForumGroup(Forum forum)
        {
            this.ID = forum.ID;
            this.Title = forum.Title;
            this.ParentID = forum.ParentID;
            this.Description = forum.Description;
            this.Pos = forum.Pos;
            thisForum = forum;
        }

        public ForumGroup(Forum forum, IForumContext _context)
            : this(forum)
        {
            context = _context;
          //  repository = forumRepository;
        }

        public bool HasChildren
        {
            get
            {
                return context.Forums.HasChildren(this.ID);
            }
        }

        public IEnumerable<ForumGroup> SubGroups
        {
            get
            {
                return context.Forums.Filter(f => f.IsGroup && f.ParentID == this.ID)
                    .OrderBy(f=>f.Pos)
                    .ToList()
                    .Select(f => new ForumGroup(f, context));
            }
        }

        public IEnumerable<ForumWrapper> SubForums
        {
            get
            {
                return context.Forums.Filter(f => f.ParentID == ID && !f.IsGroup)
                    .OrderByDescending(f => f.Pos)
                    .ToList()
                    .Select(f => new ForumWrapper(f, context));
            }
        }

        //public Uri Url 
        //{
        //    get 
        //    {
        //        return new Uri(WebSite.ApplicationPath + "/community/g-" + ID.ToString() + ".html");
        //    }
        //}
    }

}